package experiments;

/**
 * @authors Owen Phelan
 *----------------------------------------------------------------
 * IOpreferences.java: This will take in the variable values 
 * assigned by the user in the PreferencesGUI and save them into 
 * compSimProperties.txt and defaultCompSimProperties.txt
 *----------------------------------------------------------------
 */

import java.io.*;
import java.util.Properties;
import java.awt.Color;

import preferences.Preferences;

public class IOpreferences{

//-------------------------------------------------------------------------------------------
//           IOpreferences Fields
//-------------------------------------------------------------------------------------------
//CPU fields
	
	/**
	 * Time taken for each instruction to be executed.
	 */	
	private  int CPUInstructionTime;
	private  String CPUInstructionTimeString;
		
	
	/**
	 * Number of ALU's present in the CPU.
	 */	
	private  int numALU;
	private  String numALUString;
		
	/**
	 * Maximum number of istuctions that can be carried
	 * out at any particular time.
	 */	
	private  int instructionMax;
	private  String instructionMaxString;
	
//-------------------------------------------------------------------------------------------
//Monitoring Fields
	
	/**
	 *  CPU monitoring status. i.e. is
	 *  CPU being monitored
	 */
	private  boolean CPUMon;
	private  String CPUMonitoringStatusString;
	
	/**
	 * RAM monitoring status
	 */
	private  boolean RAMMon;
	private  String RAMMonitoringStatusString;
	
	/**
	 * instruction set monitoring status
	 */
	private  boolean instructionMon;
	private  String instructionSetMonitoringStatusString;
	
	/**
	 *	cache monitoring status
	 */
	private  boolean cacheMon;
	private  String cacheMonitoringStatusString;
	
//-------------------------------------------------------------------------------------------
//RAM Fields
	
	/**
	 * Size of the RAM
	 */
	private  int ramSize;
	private  String ramSizeString;
	
	/**
	 * Speed of the RAM
	 */
	private  int ramSpeed;
	private  String ramSpeedString;
	
//------------------------------------------------------------------------------------------
//Main GUI Fields
	
	/**
	 * Size of the text
	 */
	private  int textSize;
	private  String textSizeString;
	
	/**
	 * Colour of the text
	 */
	private int textRed;
	private int textGreen;
	private int textBlue;
	private String textRedString;
	private String textGreenString;
	private String textBlueString;
	private  String textColour;
	private  String textColourString;
		
	/**
	 * Colour of the background
	 */
	private  String bgColour;
	private  String bgColourString;
		
	/**
	 * Background Image
	 */
	private String bgImage;
	private String bgImageString;

//------------------------------------------------------------------------------------------
	
	/**
	 * Retrieves the Properties
	 */
	
	public IOpreferences(){
		retrieveProperties();		
	}
	
//-------------------------------------------------------------------------------------------
//Cache Fields
	
	/**
	 * Status of the L2 Cache
	 */
	private  boolean L2Status;
	private String L2StatusString;

	/**
	 * Size of L1
	 */
	private  int L1CacheSize;
	private String L1CacheSizeString;

	/**
	 * Size of L2
	 */
	private  int L2CacheSize;
	private String L2CacheSizeString;

	/**
	 * Speed of L1
	 */
	private  int L1CacheSpeed;
	private String L1CacheSpeedString;

	/**
	 * Speed of L2
	 */
	private  int L2CacheSpeed;
	private String L2CacheSpeedString;
	
//-------------------------------------------------------------------------------------------
//Text I/O Field
	
	/**
	 * Returns whether the TEXT I/O log is enabled
	 */
	private boolean isTextIOEnabled;
	private String isTextIOEnabledString;

//-------------------------------------------------------------------------------------------
//Report Fields
	
	/**
	 *  CPU Report status. i.e. is
	 *  CPU being reported
	 */
	private  boolean CPURep;
	private  String CPUReportStatusString;
	
	/**
	 * RAM report status
	 */
	private  boolean RAMRep;
	private  String RAMReportStatusString;
	
	/**
	 * instruction set report status
	 */
	private  boolean instructionRep;
	private  String instructionSetReportStatusString;
	
	/**
	 *	cache report status
	 */
	private  boolean cacheRep;
	private  String cacheReportStatusString;

//-------------------------------------------------------------------------------------------
//Permanent storage
	
	/**
	 * permanent storage speed
	 */
	private int PSSpeed;
	private String PSSpeedString;
	
	/**
	 * permanent storage capacity
	 */
	private int PSCapacity;
	private String PSCapacityString;

//-------------------------------------------------------------------------------------------
//Instruction Set
	
	/**
	 * Instruction Set speed
	 */
	private int ISSpeed;
	private String ISSpeedString;
	
	/**
	 * Instruction set type
	 */
	private int ISType;
	private String ISTypeString;
	
//-------------------------------------------------------------------------------------------

	/**
	 * Retrieves Properties from compSimProperties.txt
	 */
	
	public void retrieveProperties(){


			File inFile = new File("compSimProperties.txt");
			FileInputStream in;

			Properties CompSimProp = new Properties();
			

			try
			{
			    in = new FileInputStream(inFile);
			    CompSimProp.load(in);
			}
			catch(java.io.IOException ioe){
				System.err.println("Property loading error");
			}
			
			String loaded = null;
			loaded = CompSimProp.getProperty("instructionMax");
			this.instructionMax = Integer.parseInt(loaded);
			loaded = CompSimProp.getProperty("CPUInstructionTime");
			this.CPUInstructionTime=Integer.parseInt(loaded);
			loaded = CompSimProp.getProperty("ALUCount");
			this.numALU = Integer.parseInt(loaded);
			loaded = CompSimProp.getProperty("CPUMonitoringStatus");
			this.CPUMon = stringtoboolean(loaded);
			loaded = CompSimProp.getProperty("cacheMonitoringStatus");
			this.cacheMon = stringtoboolean(loaded);
			loaded = CompSimProp.getProperty("instructionSetMonitoringStatus");
			this.instructionMon = stringtoboolean(loaded);
			loaded = CompSimProp.getProperty("RAMMonitoringStatus");
			this.RAMMon = stringtoboolean(loaded);
			loaded = CompSimProp.getProperty("CPUReportStatus");
			this.CPURep = stringtoboolean(loaded);
			loaded = CompSimProp.getProperty("cacheReportStatus");
			this.cacheRep = stringtoboolean(loaded);
			loaded = CompSimProp.getProperty("instructionSetReportStatus");
			this.instructionRep = stringtoboolean(loaded);
			loaded = CompSimProp.getProperty("RAMReportStatus");
			this.RAMRep = stringtoboolean(loaded);
			loaded = CompSimProp.getProperty("ramSize");
			this.ramSize = Integer.parseInt(loaded);
			loaded = CompSimProp.getProperty("ramSpeed");
			this.ramSpeed = Integer.parseInt(loaded);
			loaded = CompSimProp.getProperty("l1size");
			this.L1CacheSize = Integer.parseInt(loaded);
			loaded = CompSimProp.getProperty("l2size");
			this.L2CacheSize = Integer.parseInt(loaded);
			loaded = CompSimProp.getProperty("l1speed");
			this.L1CacheSpeed = Integer.parseInt(loaded);
			loaded = CompSimProp.getProperty("l2speed");
			this.L2CacheSpeed = Integer.parseInt(loaded);
			loaded = CompSimProp.getProperty("l2status");
			this.L2Status = stringtoboolean(loaded);
			loaded = CompSimProp.getProperty("bgColour");
			this.bgColour = loaded;
			loaded = CompSimProp.getProperty("textSize");
			this.textSize = Integer.parseInt(loaded);
			loaded = CompSimProp.getProperty("textRed");
			this.textRed = Integer.parseInt(loaded);
			loaded = CompSimProp.getProperty("textGreen");
			this.textGreen = Integer.parseInt(loaded);
			loaded = CompSimProp.getProperty("textBlue");
			this.textBlue = Integer.parseInt(loaded);
			loaded = CompSimProp.getProperty("textIOStatus");
			this.isTextIOEnabled = stringtoboolean(loaded);
			loaded = CompSimProp.getProperty("bgImage");
			this.bgImage = loaded;
			loaded = CompSimProp.getProperty("PermanentStorageSpeed");
			this.PSSpeed = Integer.parseInt(loaded);
			loaded = CompSimProp.getProperty("PermanentStorageCapacity");
			this.PSCapacity = Integer.parseInt(loaded);
			loaded = CompSimProp.getProperty("InstructionSetSpeed");
			this.ISSpeed = Integer.parseInt(loaded);
			loaded = CompSimProp.getProperty("InstructionSetType");
			this.ISType = Integer.parseInt(loaded);
			//PreferencesGUI.prefs = new Preferences(this.CPUMon,this.RAMMon,this.cacheMon,this.instructionMon,this.instructionMax,this.CPUInstructionTime,this.numALU,this.ramSize,this.ramSpeed,this.L1CacheSize, this.L2CacheSize, this.L1CacheSpeed, this.L2CacheSpeed, this.CPURep,this.RAMRep,this.cacheRep,this.instructionRep,this.bgColour,this.textSize,this.textRed,this.textGreen,this.textBlue, this.L2Status, this.isTextIOEnabled,this.bgImage,this.PSSpeed,this.PSCapacity,this.ISSpeed,this.ISType);
	}
	
//-------------------------------------------------------------------------------------------
	
	/**
	 * The following retrives the default properties from the
	 * txt file they are saved into and sets their associated
	 * variables the default properties.
	 */
	public void retrieveDefaultProperties(){


		File inFile = new File("defaultCompSimProperties.txt");
		FileInputStream in;

		Properties CompSimProp = new Properties();
		

		try
		{
		    in = new FileInputStream(inFile);
		    CompSimProp.load(in);
		}
		catch(java.io.IOException ioe){
			System.err.println("Property loading error");
		}
		
		String loaded = null;
		loaded = CompSimProp.getProperty("instructionMax");
		this.instructionMax = Integer.parseInt(loaded);
		loaded = CompSimProp.getProperty("CPUInstructionTime");
		this.CPUInstructionTime=Integer.parseInt(loaded);
		loaded = CompSimProp.getProperty("ALUCount");
		this.numALU = Integer.parseInt(loaded);
		loaded = CompSimProp.getProperty("CPUMonitoringStatus");
		this.CPUMon = stringtoboolean(loaded);
		loaded = CompSimProp.getProperty("cacheMonitoringStatus");
		this.cacheMon = stringtoboolean(loaded);
		loaded = CompSimProp.getProperty("instructionSetMonitoringStatus");
		this.instructionMon = stringtoboolean(loaded);
		loaded = CompSimProp.getProperty("RAMMonitoringStatus");
		this.RAMMon = stringtoboolean(loaded);
		loaded = CompSimProp.getProperty("ramSize");
		this.ramSize = Integer.parseInt(loaded);
		loaded = CompSimProp.getProperty("ramSpeed");
		this.ramSpeed = Integer.parseInt(loaded);
		loaded = CompSimProp.getProperty("l1size");
		this.L1CacheSize = Integer.parseInt(loaded);
		loaded = CompSimProp.getProperty("l2size");
		this.L2CacheSize = Integer.parseInt(loaded);
		loaded = CompSimProp.getProperty("l1speed");
		this.L1CacheSpeed = Integer.parseInt(loaded);
		loaded = CompSimProp.getProperty("l2speed");
		this.L2CacheSpeed = Integer.parseInt(loaded);
		loaded = CompSimProp.getProperty("CPUReportStatus");
		this.CPURep = stringtoboolean(loaded);
		loaded = CompSimProp.getProperty("cacheReportStatus");
		this.cacheRep = stringtoboolean(loaded);
		loaded = CompSimProp.getProperty("instructionSetReportStatus");
		this.instructionRep = stringtoboolean(loaded);
		loaded = CompSimProp.getProperty("RAMReportStatus");
		this.RAMRep = stringtoboolean(loaded);
		loaded = CompSimProp.getProperty("bgColour");
		this.bgColour = loaded;
		loaded = CompSimProp.getProperty("textSize");
		this.textSize = Integer.parseInt(loaded);
		loaded = CompSimProp.getProperty("textRed");
		this.textRed = Integer.parseInt(loaded);
		loaded = CompSimProp.getProperty("textGreen");
		this.textGreen = Integer.parseInt(loaded);
		loaded = CompSimProp.getProperty("textBlue");
		this.textBlue = Integer.parseInt(loaded);
		loaded = CompSimProp.getProperty("textIOStatus");
		this.isTextIOEnabled = stringtoboolean(loaded);
		loaded = CompSimProp.getProperty("bgImage");
		this.bgImage = loaded;
		loaded = CompSimProp.getProperty("PermanentStorageSpeed");
		this.PSSpeed = Integer.parseInt(loaded);
		loaded = CompSimProp.getProperty("PermanentStorageCapacity");
		this.PSCapacity = Integer.parseInt(loaded);
		loaded = CompSimProp.getProperty("InstructionSetSpeed");
		this.ISSpeed = Integer.parseInt(loaded);
		loaded = CompSimProp.getProperty("InstructionSetType");
		this.ISType = Integer.parseInt(loaded);
		//PreferencesGUI.prefs = new Preferences(this.CPUMon,this.RAMMon,this.cacheMon,this.instructionMon,this.instructionMax,this.CPUInstructionTime,this.numALU,this.ramSize,this.ramSpeed,this.L1CacheSize, this.L2CacheSize, this.L1CacheSpeed, this.L2CacheSpeed, this.CPURep,this.RAMRep,this.cacheRep,this.instructionRep,this.bgColour,this.textSize,this.textRed,this.textGreen,this.textBlue,this.L2Status, this.isTextIOEnabled,this.bgImage,this.PSSpeed,this.PSCapacity,this.ISSpeed,this.ISType);
}

//-------------------------------------------------------------------------------------------

	/**
	 * the following will set the values to their
	 * variables so they can be saved.
	 */
	
	public  void saveProperties(){
		
		/**
		 * CPU property conversion
		 */
		Integer tempCPUInstructionTime;
		tempCPUInstructionTime = new Integer(PreferencesGUI.prefs.getCPUInstructionTime());
		CPUInstructionTimeString = tempCPUInstructionTime.toString();
		
		Integer tempNumALU;
		tempNumALU = new Integer(PreferencesGUI.prefs.getNumALU());
		numALUString = tempNumALU.toString();
		
		Integer tempInstructionMax;
		tempInstructionMax = new Integer(PreferencesGUI.prefs.getInstructionMax());
		instructionMaxString = tempInstructionMax.toString();
		
		/**
		 * Monitoring property conversion
		 */
		
		Boolean tempCPUMon;
		tempCPUMon=new Boolean(PreferencesGUI.prefs.getCPUMonitorStatus());
		CPUMonitoringStatusString=tempCPUMon.toString();

		Boolean tempRAMMon;
		tempRAMMon=new Boolean(PreferencesGUI.prefs.getRAMMonitoringStatus());
		RAMMonitoringStatusString=tempRAMMon.toString();
		
		Boolean tempInstructSetMon;
		tempInstructSetMon=new Boolean(PreferencesGUI.prefs.getInstructionSetMonitoringStatus());
		instructionSetMonitoringStatusString=tempInstructSetMon.toString();
		
		Boolean tempCacheMon;
		tempCacheMon=new Boolean(PreferencesGUI.prefs.getCacheMonitoringStatus());
		cacheMonitoringStatusString=tempCacheMon.toString();
		
		/**
		 * Report property conversion
		 */
		
		Boolean tempCPURep;
		tempCPURep=new Boolean(PreferencesGUI.prefs.getCPUReportStatus());
		CPUReportStatusString=tempCPURep.toString();

		Boolean tempRAMRep;
		tempRAMRep=new Boolean(PreferencesGUI.prefs.getRAMReportStatus());
		RAMReportStatusString=tempRAMRep.toString();
		
		Boolean tempInstructSetRep;
		tempInstructSetRep=new Boolean(PreferencesGUI.prefs.getInstructionSetReportStatus());
		instructionSetReportStatusString=tempInstructSetRep.toString();
		
		Boolean tempCacheRep;
		tempCacheRep=new Boolean(PreferencesGUI.prefs.getCacheReportStatus());
		cacheReportStatusString=tempCacheRep.toString();
		
		
		/**
		 * RAM property conversion
		 */
		
		Integer tempRAMSize;
		tempRAMSize = new Integer(PreferencesGUI.prefs.getRAMSize());
		ramSizeString = tempRAMSize.toString();
		
		Integer tempRAMSpeed;
		tempRAMSpeed = new Integer(PreferencesGUI.prefs.getRAMSpeed());
		ramSpeedString = tempRAMSpeed.toString();
		
		/**
		 * Cache
		 */
		Integer tempL1Size;
		tempL1Size=new Integer(PreferencesGUI.prefs.getL1CacheSize());
		L1CacheSizeString=tempL1Size.toString();
		
		Integer tempL2Size;
		tempL2Size=new Integer(PreferencesGUI.prefs.getL2CacheSize());
		L2CacheSizeString=tempL2Size.toString();
		
		Integer tempL1Speed;
		tempL1Speed=new Integer(PreferencesGUI.prefs.getL1CacheSpeed());
		L1CacheSpeedString=tempL1Speed.toString();
		
		Integer tempL2Speed;
		tempL2Speed=new Integer(PreferencesGUI.prefs.getL2CacheSpeed());
		L2CacheSpeedString=tempL2Speed.toString();

		Boolean tempL2Status;
		tempL2Status=new Boolean(PreferencesGUI.prefs.isL2Enabled());
		L2StatusString = tempL2Status.toString();
		
		/**
		 * Text I/O
		 */
		
		Boolean tempTextIOStatus;
		tempTextIOStatus = new Boolean(PreferencesGUI.prefs.getTextIOStatus());
		isTextIOEnabledString = tempTextIOStatus.toString();
		
		 
		/**
		 * Main GUI property conversion
		 */
		Integer tempTextSize;
		tempTextSize = new Integer(PreferencesGUI.prefs.getTextSize());
		textSizeString = tempTextSize.toString();
		
		Integer tempTextRed;
		tempTextRed = new Integer(PreferencesGUI.prefs.getTextRed());
		textRedString = tempTextRed.toString();
		
		Integer tempTextGreen;
		tempTextGreen = new Integer(PreferencesGUI.prefs.getTextGreen());
		textGreenString = tempTextGreen.toString();
		
		Integer tempTextBlue;
		tempTextBlue = new Integer(PreferencesGUI.prefs.getTextBlue());
		textBlueString = tempTextBlue.toString();
		
		String tempBGColour;
		tempBGColour = new String(PreferencesGUI.prefs.getBGColour());
		bgColourString = tempBGColour;
		
		String tempBGImage;
		tempBGImage = new String(PreferencesGUI.prefs.getBGImage());
		bgImageString = tempBGImage;
		
		/**
		 * Permanent Storage property conversion
		 */
		Integer tempPSSpeed;
		tempPSSpeed = new Integer(PreferencesGUI.prefs.getPermanentStorageSpeed());
		PSSpeedString = tempPSSpeed.toString();
		
		Integer tempPSCapacity;
		tempPSCapacity = new Integer(PreferencesGUI.prefs.getPermanentStorageCapacity());
		PSCapacityString = tempPSCapacity.toString();
		
		/**
		 * Instruction Set property conversion
		 */
		Integer tempISSpeed;
		tempISSpeed = new Integer(PreferencesGUI.prefs.getInstructionSetSpeed());
		ISSpeedString = tempISSpeed.toString();
		
		Integer tempISType;
		tempISType = new Integer(PreferencesGUI.prefs.getInstructionSetType());
		ISTypeString = tempISType.toString();
		
//-------------------------------------------------------------------------------------------

		/**
		* Save all(String Form) properties to disk.
		*/
				
			Properties CompSimProp=new Properties();
			
				
			//CPU input properties
			CompSimProp.setProperty("CPUInstructionTime",CPUInstructionTimeString);
			CompSimProp.setProperty("ALUCount",numALUString);
			CompSimProp.setProperty("instructionMax",instructionMaxString);
			
			//Monitoring properties
			CompSimProp.setProperty("CPUMonitoringStatus",CPUMonitoringStatusString);
			CompSimProp.setProperty("cacheMonitoringStatus",cacheMonitoringStatusString);
			CompSimProp.setProperty("instructionSetMonitoringStatus",instructionSetMonitoringStatusString);
			CompSimProp.setProperty("RAMMonitoringStatus",RAMMonitoringStatusString);	
			
			//RAM Properties
			CompSimProp.setProperty("ramSize",ramSizeString);
			CompSimProp.setProperty("ramSpeed",ramSpeedString);
			
			//Cache Properties
			CompSimProp.setProperty("l1size",L1CacheSizeString);
			CompSimProp.setProperty("l2size",L2CacheSizeString);
			CompSimProp.setProperty("l1speed",L1CacheSpeedString);
			CompSimProp.setProperty("l2speed",L2CacheSpeedString);
			CompSimProp.setProperty("l2status",L2StatusString);
			
			//Text I/O Properties
			CompSimProp.setProperty("textIOStatus", isTextIOEnabledString);
			
			//Main GUI Properties
			CompSimProp.setProperty("textSize",textSizeString);
			CompSimProp.setProperty("textRed",textRedString);
			CompSimProp.setProperty("textGreen",textGreenString);
			CompSimProp.setProperty("textBlue",textBlueString);
			CompSimProp.setProperty("bgColour",bgColourString);
			CompSimProp.setProperty("bgImage",bgImageString);
			
            //Report properties
			CompSimProp.setProperty("CPUReportStatus",CPUReportStatusString);
			CompSimProp.setProperty("cacheReportStatus",cacheReportStatusString);
			CompSimProp.setProperty("instructionSetReportStatus",instructionSetReportStatusString);
			CompSimProp.setProperty("RAMReportStatus",RAMReportStatusString);	
			
			//Permanent Storage
			CompSimProp.setProperty("PermanentStorageSpeed", PSSpeedString);
			CompSimProp.setProperty("PermanentStorageCapacity", PSCapacityString);
			
			//Instruction Set
			CompSimProp.setProperty("InstructionSetSpeed", ISSpeedString);
			CompSimProp.setProperty("InstructionSetType", ISTypeString);
			
			//create file to save to
			File save=new File("compSimProperties.txt");
			
			//create stream
			OutputStream compSimProps=null;
				
			//try-catch in case of ioException
			try{		
				compSimProps=new FileOutputStream(save);		
			}catch(IOException ioe){
				System.err.println("Problem occured when trying to save properties.");
				System.err.println("PROPERTIES NOT SAVED");
			}
				
			//save properties to file	
				
			try{
				CompSimProp.store(compSimProps,"Test Properties Download, by Owen Grogan and Goncalo");
			}catch(Exception ioe){
				System.err.println("Problem occured when trying to save properties.");
				System.err.println("PROPERTIES NOT SAVED");
			}		
	}

//-------------------------------------------------------------------------------------------
	
	/**
	 * The following is a piece of code
	 * which converts a string to a boolean
	 * value.
	 */

	public boolean stringtoboolean(String s){
		boolean b;
		
		if(s.equals("false")){
			b=false;
		}else{
			b=true;
		}
		return b;
	}
			
}